#!/usr/bin/env python
import webapp2

from webapp2_extras import jinja2
from jinja2.filters import do_pprint

class Jinja2Handler(webapp2.RequestHandler):

    @webapp2.cached_property
    def jinja2(self):
        return jinja2.get_jinja2(app=self.app)


    def render_template(self, template_name, params={}):
        title = 'Quick Serve Infosystems Ltd'
        css_body = 'homepage' if self.uri_for('home') else 'default'
        params.update({'title':title,'css_body': css_body})
        warning = self.request.get('warning')
        message = self.request.get('message')
        if warning:
            params.update({'warning': warning})
        if warning:
            params.update({'message': message})
        self.response.write(self.jinja2.render_template(
            template_name, **params))

    def display_message(self, message):
        """Utility function to display a template with a simple message."""
        params = {
          'message': message
        }
        self.render_template('message.html', params)
        

    def render_string(self, template_string, params={}):
        self.response.write(self.jinja2.environment.from_string(
            template_string).render(**params))

    def json_response(self, json):
        self.response.headers.add_header('content-type', 'application/json', charset='utf-8')
        self.response.out.write(json)
        
class MainHandler(Jinja2Handler):
    def get(self):
        params = {}
        self.render_template('homepage.html',params)

class ContactHandler(Jinja2Handler):
    def get(self):
        params = {}
        self.render_template('contact.html',params)

class LoginHandler(Jinja2Handler):
    def get(self):
        params = {}
        self.render_template('login.html',params)

config = {
  'webapp2_extras.jinja2': {
        'filters': {
            'do_pprint': do_pprint,
            },
        },
}

app = webapp2.WSGIApplication([
    webapp2.Route('/', MainHandler, name='home'),
    webapp2.Route('/contact', ContactHandler, name='contact'),
    webapp2.Route('/login', LoginHandler, name='login')
], debug=True, config=config)

#logging.getLogger().setLevel(logging.DEBUG)
